namespace DotNetNuke.UI.WebControls
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Xml;

    public class MenuNode : DNNNode
    {
        private DotNetNuke.UI.WebControls.DNNMenu m_objDNNMenu;
        private MenuNodeCollection m_objNodes;

        public MenuNode()
        {
        }

        public MenuNode(string strText) : base(strText)
        {
        }

        internal MenuNode(Control ctlOwner)
        {
            this.m_objDNNMenu = (DotNetNuke.UI.WebControls.DNNMenu) ctlOwner;
        }

        internal MenuNode(XmlNode objXmlNode, Control ctlOwner) : base(objXmlNode)
        {
            this.m_objDNNMenu = (DotNetNuke.UI.WebControls.DNNMenu) ctlOwner;
        }

        public void Click()
        {
            this.Selected = !this.Selected;
            if (this.DNNMenu.IsDownLevel)
            {
                foreach (MenuNode node in this.DNNMenu.SelectedMenuNodes)
                {
                    if (node.Level > this.Level)
                    {
                        node.Selected = false;
                    }
                }
            }
            this.DNNMenu.OnNodeClick(new DNNMenuNodeClickEventArgs(this));
        }

        public virtual void Render(HtmlTextWriter writer)
        {
            this.NodeWriter.RenderNode(writer, this);
        }

        internal void SetDNNMenu(DotNetNuke.UI.WebControls.DNNMenu objMenu)
        {
            this.m_objDNNMenu = objMenu;
        }

        [DefaultValue(""), Bindable(true), PersistenceMode(PersistenceMode.Attribute)]
        public string CssClassOver
        {
            get
            {
                return this.CSSClassHover;
            }
            set
            {
                this.CSSClassHover = value;
            }
        }

        [Browsable(false)]
        public DotNetNuke.UI.WebControls.DNNMenu DNNMenu
        {
            get
            {
                return this.m_objDNNMenu;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(-1)]
        public int ImageIndex
        {
            get
            {
                if (Strings.Len(this.get_CustomAttribute("imgIdx")) > 0)
                {
                    return IntegerType.FromString(this.get_CustomAttribute("imgIdx"));
                }
                return -1;
            }
            set
            {
                this.set_CustomAttribute("imgIdx", StringType.FromInteger(value));
            }
        }

        public string LeftHTML
        {
            get
            {
                return this.get_CustomAttribute("lhtml", "");
            }
            set
            {
                this.set_CustomAttribute("lhtml", value);
            }
        }

        [Browsable(true), PersistenceMode(PersistenceMode.InnerProperty)]
        public MenuNodeCollection MenuNodes
        {
            get
            {
                if (this.m_objNodes == null)
                {
                    this.m_objNodes = new MenuNodeCollection(this.XmlNode, this.DNNMenu);
                }
                return this.m_objNodes;
            }
        }

        private IMenuNodeWriter NodeWriter
        {
            get
            {
                if (this.m_objDNNMenu.IsDownLevel)
                {
                    return new MenuNodeWriter();
                }
                return null;
            }
        }

        [Browsable(false)]
        public MenuNode Parent
        {
            get
            {
                return this.ParentNode;
            }
        }

        public MenuNode ParentNode
        {
            get
            {
                if ((this.XmlNode.ParentNode != null) && (this.XmlNode.ParentNode.NodeType != XmlNodeType.Document))
                {
                    return new MenuNode(this.XmlNode.ParentNode, this.m_objDNNMenu);
                }
                return null;
            }
        }

        public string RightHTML
        {
            get
            {
                return this.get_CustomAttribute("rhtml", "");
            }
            set
            {
                this.set_CustomAttribute("rhtml", value);
            }
        }
    }
}

